package com.example.mylibrary.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.example.mylibrary.data.ItemGrop;
import com.example.mylibrary.data.StudentItem;

import com.example.mylibrary.data.ItemRollCall;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "ITEM_ROLL_CALL".
*/
public class ItemRollCallDao extends AbstractDao<ItemRollCall, Long> {

    public static final String TABLENAME = "ITEM_ROLL_CALL";

    /**
     * Properties of entity ItemRollCall.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property ItemRollCallID = new Property(0, Long.class, "itemRollCallID", true, "_id");
        public final static Property ItemGropID = new Property(1, long.class, "itemGropID", false, "ITEM_GROP_ID");
        public final static Property StudentItemID = new Property(2, long.class, "studentItemID", false, "STUDENT_ITEM_ID");
        public final static Property FacePath = new Property(3, String.class, "facePath", false, "FACE_PATH");
        public final static Property RollCallState = new Property(4, int.class, "rollCallState", false, "ROLL_CALL_STATE");
        public final static Property RollCalltime = new Property(5, String.class, "rollCalltime", false, "ROLL_CALLTIME");
    }

    private DaoSession daoSession;


    public ItemRollCallDao(DaoConfig config) {
        super(config);
    }
    
    public ItemRollCallDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"ITEM_ROLL_CALL\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: itemRollCallID
                "\"ITEM_GROP_ID\" INTEGER NOT NULL ," + // 1: itemGropID
                "\"STUDENT_ITEM_ID\" INTEGER NOT NULL ," + // 2: studentItemID
                "\"FACE_PATH\" TEXT," + // 3: facePath
                "\"ROLL_CALL_STATE\" INTEGER NOT NULL ," + // 4: rollCallState
                "\"ROLL_CALLTIME\" TEXT);"); // 5: rollCalltime
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"ITEM_ROLL_CALL\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, ItemRollCall entity) {
        stmt.clearBindings();
 
        Long itemRollCallID = entity.getItemRollCallID();
        if (itemRollCallID != null) {
            stmt.bindLong(1, itemRollCallID);
        }
        stmt.bindLong(2, entity.getItemGropID());
        stmt.bindLong(3, entity.getStudentItemID());
 
        String facePath = entity.getFacePath();
        if (facePath != null) {
            stmt.bindString(4, facePath);
        }
        stmt.bindLong(5, entity.getRollCallState());
 
        String rollCalltime = entity.getRollCalltime();
        if (rollCalltime != null) {
            stmt.bindString(6, rollCalltime);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, ItemRollCall entity) {
        stmt.clearBindings();
 
        Long itemRollCallID = entity.getItemRollCallID();
        if (itemRollCallID != null) {
            stmt.bindLong(1, itemRollCallID);
        }
        stmt.bindLong(2, entity.getItemGropID());
        stmt.bindLong(3, entity.getStudentItemID());
 
        String facePath = entity.getFacePath();
        if (facePath != null) {
            stmt.bindString(4, facePath);
        }
        stmt.bindLong(5, entity.getRollCallState());
 
        String rollCalltime = entity.getRollCalltime();
        if (rollCalltime != null) {
            stmt.bindString(6, rollCalltime);
        }
    }

    @Override
    protected final void attachEntity(ItemRollCall entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public ItemRollCall readEntity(Cursor cursor, int offset) {
        ItemRollCall entity = new ItemRollCall( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // itemRollCallID
            cursor.getLong(offset + 1), // itemGropID
            cursor.getLong(offset + 2), // studentItemID
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // facePath
            cursor.getInt(offset + 4), // rollCallState
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // rollCalltime
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, ItemRollCall entity, int offset) {
        entity.setItemRollCallID(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setItemGropID(cursor.getLong(offset + 1));
        entity.setStudentItemID(cursor.getLong(offset + 2));
        entity.setFacePath(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setRollCallState(cursor.getInt(offset + 4));
        entity.setRollCalltime(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(ItemRollCall entity, long rowId) {
        entity.setItemRollCallID(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(ItemRollCall entity) {
        if(entity != null) {
            return entity.getItemRollCallID();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(ItemRollCall entity) {
        return entity.getItemRollCallID() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getItemGropDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getStudentItemDao().getAllColumns());
            builder.append(" FROM ITEM_ROLL_CALL T");
            builder.append(" LEFT JOIN ITEM_GROP T0 ON T.\"ITEM_GROP_ID\"=T0.\"_id\"");
            builder.append(" LEFT JOIN STUDENT_ITEM T1 ON T.\"STUDENT_ITEM_ID\"=T1.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ItemRollCall loadCurrentDeep(Cursor cursor, boolean lock) {
        ItemRollCall entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ItemGrop itemGrop = loadCurrentOther(daoSession.getItemGropDao(), cursor, offset);
         if(itemGrop != null) {
            entity.setItemGrop(itemGrop);
        }
        offset += daoSession.getItemGropDao().getAllColumns().length;

        StudentItem studentItemid = loadCurrentOther(daoSession.getStudentItemDao(), cursor, offset);
         if(studentItemid != null) {
            entity.setStudentItemid(studentItemid);
        }

        return entity;    
    }

    public ItemRollCall loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ItemRollCall> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ItemRollCall> list = new ArrayList<ItemRollCall>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ItemRollCall> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ItemRollCall> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
